package com.dhcc.wb.qiye;

import java.util.List;

import org.apache.log4j.Logger;

import com.dhcc.common.database.DBManager;
import com.dhcc.common.util.StringUtil;

public class QiyeDao {
	private static final Logger logger = Logger.getLogger(QiyeDao.class);
	
	/*
	 * 查询所有
	 */
	@SuppressWarnings("unchecked")
	public List<QiyeModel> getAll() {
		DBManager dbm = null;
		List<QiyeModel> menuModel = null;
		try {
			dbm = new DBManager();
			String sql = "SELECT * from weixin_qiye order by px";
			menuModel = dbm.getObjectList(QiyeModel.class, sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return menuModel;
	}
	
	public Integer queryCount(String name) {
		DBManager dbm = null;
		Integer count = 0;
		try {
			dbm = new DBManager();
			String sql = "select count(*) from weixin_qiye g where 1=1 ";
			if(!StringUtil.isNullOrEmpty(name)){
				sql += "and g.qiyename like '%"+ name +"%'";
			}
			count = dbm.executeQueryCount(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return count;
	}
	
	
	
	
	/*
	 * 查询
	 */
	@SuppressWarnings("unchecked")
	public List<QiyeModel> findQiyeServiceByPage(Integer pageNumber,Integer pageSize,String name) {
		DBManager dbm = null;
		List<QiyeModel> list = null;
		try {
			dbm = new DBManager();
			String sql = "select * from weixin_qiye where 1=1 ";
				   if(!StringUtil.isNullOrEmpty(name)){
					   sql += " and qiyename LIKE '%"+ name +"%'";			   
				   }
				   sql += " order by px";
			       sql +=  " limit " + pageNumber + ","+ pageSize +"";
			list = dbm.getObjectList(QiyeModel.class, sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return list;
	}
	
	//删除
		public int delete(String _id) {
			int i =0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "delete from weixin_qiye where qiyeid = '"+ _id +"'";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
	//编辑
		public QiyeModel queryqiye_result(String _id) {
			QiyeModel model = null;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select * from weixin_qiye qiye "
						+ " where qiye.qiyeid = '"+ _id +"'";
				model = (QiyeModel) dbm.getObject(QiyeModel.class, sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return model;
		}
		
		//编辑
		public QiyeModel queryqiyeByname_result(String name) {
			QiyeModel model = null;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select * from weixin_qiye qiye "
						+ " where qiye.qiyename = '"+ name +"'";
				model = (QiyeModel) dbm.getObject(QiyeModel.class, sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return model;
		}
		
		//新增
		public int addQiye(String qiyeid, String qiyename,Integer px,String beizhu,String parentid) {
			int i=0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "insert into weixin_qiye(qiyeid,qiyename,px,beizhu,parentid) values('"+ qiyeid +"','"+ qiyename +"','"+ px +"','"+beizhu+"','"+parentid+"')";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		//更新
		public int updateQiye(String qiyeid, String qiyename,Integer px,String beizhu,String parentid) {
			int i = 0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "update weixin_qiye set qiyename = '"+ qiyename +"',px='"+px+"',beizhu='"+beizhu+"',parentid='"+parentid+"' where qiyeid = '"+ qiyeid +"'";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
}
